package cn.mistorange.controller;

import cn.mistorange.pojo.Result;
import cn.mistorange.pojo.User;
import cn.mistorange.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    //添加新用户
    @PostMapping
    public Result AddUser(@RequestBody User user){
        log.info("user: {}", user);
        userService.createUser(user);
        return Result.success();
    }

    //批量删除用户
    @DeleteMapping("/{ids}")
    public Result deleteUser(@PathVariable List<Integer> ids){
        userService.deleteUser(ids);
        return Result.success();
    }

    //根据id查询
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id){
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    //用户修改
    @PutMapping
    public Result updateUser(User user){
        userService.updateUser(user);
        return Result.success();
    }
}
